// example classDemo() { val name = "tom" val age = 18 doSomething() // 初始化对象时,会打印 name: tome, age: 18 defdoSomething() = { println("name: " + name + ", age: " + age) } }
辅助构造器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classDemo{ var name = "" var age = 0 // 错误定义!! defthis() { } defthis(name: String) { this() this.name = name } defthis(name: String, age: Int) { this(name) this.age = age } }